package com.zf.yaml_properties_ini;

import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.SubnodeConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * .ini 文件的读写
 */
public class IniReadWriteTest {
    public static void main(String[] args) throws IOException, ConfigurationException {
        // 测试读取
        testRead();

        // 测试写入
        testWrite();
    }

    /**
     * 测试读取
     */
    public static void testRead() throws ConfigurationException, IOException {
        ClassLoader classLoader = IniReadWriteTest.class.getClassLoader();

        // 创建ini配置
        INIConfiguration iniConfiguration = new INIConfiguration();
        // 读取ini文件
        iniConfiguration.read(new InputStreamReader(classLoader.getResourceAsStream("ini_test.ini")));

        // 获取名为 "server" 的节
        SubnodeConfiguration serverSection = iniConfiguration.getSection("server");
        // 获取节中的内容
        System.out.println(serverSection.getString("url"));
        System.out.println(serverSection.getString("Cookie"));

        // 获取名为 "say" 的节
        SubnodeConfiguration saySection = iniConfiguration.getSection("say");
        // 获取节中的内容
        System.out.println(saySection.getString("hello"));
        System.out.println(saySection.getString("hello2"));
        System.out.println(saySection.getString("hello3"));
        System.out.println(saySection.getString("hello4"));
        System.out.println(saySection.getString("hello5"));
        System.out.println(saySection.getString("url"));

        // 去读全部节和内容
        for (String sectionName : iniConfiguration.getSections()) {
            SubnodeConfiguration section = iniConfiguration.getSection(sectionName);
            section.getKeys().forEachRemaining(key -> {
                System.out.println(section.getString(key));
            });
        }

        // 按照 "节.属性" 方式读取
        System.out.println(iniConfiguration.getProperty("server.url"));
        System.out.println(iniConfiguration.getString("server.Cookie"));
        System.out.println(iniConfiguration.getString("say.hello"));
        System.out.println(iniConfiguration.getString("say.hello2"));
        System.out.println(iniConfiguration.getString("say.url"));
    }

    /**
     * 测试写入
     */
    public static void testWrite() throws ConfigurationException, IOException {
        // 创建ini配置
        INIConfiguration iniConfiguration = new INIConfiguration();

        // 添加 "server" 节
        iniConfiguration.addProperty("server.url", "http://example.com");
        iniConfiguration.addProperty("server.Cookie", "sessionid=12345");

        // 添加 "say" 节
        iniConfiguration.addProperty("say.hello", "Hello, World!");
        iniConfiguration.addProperty("say.hello2", "Hello again!");
        iniConfiguration.addProperty("say.hello3", "Hi there!");
        iniConfiguration.addProperty("say.hello4", "Greetings!");
        iniConfiguration.addProperty("say.hello5", "Salutations!");

        // addProperty
        iniConfiguration.addProperty("www.url", "http://greeting.com");

        // setProperty: 同名属性只保留最后一次设置的值
        iniConfiguration.setProperty("stu.name", "李四");
        iniConfiguration.setProperty("stu.name", "王五");

        // 写入ini文件
        FileWriter fileWriter = new FileWriter("D:\\output.ini", StandardCharsets.UTF_8);
        iniConfiguration.write(fileWriter);
        fileWriter.close();
    }
}
